package jing.app;

import java.security.Security;
import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.URLName;

public class ReceiveMail {
	public static void main(String[] args) {
		Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
		final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
		// Get a Properties object
		Properties props = System.getProperties();
		props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
		props.setProperty("mail.pop3.socketFactory.fallback", "false");
		props.setProperty("mail.pop3.port", "995");
		props.setProperty("mail.pop3.socketFactory.port", "995");
		
		Session session = Session.getInstance(props, null);
		session.setDebug(false);
		session.setDebugOut(System.out);
		
		Store store = null;
		try {
			 URLName urln = new URLName("pop3", "pop.gmail.com", 995, null, "jingz.finance@gmail.com", "xianbing");
			store = session.getStore(urln);
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
		}
		if(store != null) {
			try {
				store.connect();
				Folder f = store.getFolder("INBOX");
				if(f == null) {
					return;
				}
				f.open(Folder.READ_ONLY);
				Message[] messages = f.getMessages();
				for(Message m : messages) {
					System.out.println(m.getMessageNumber());
					System.out.println(m.getSubject());
				}
			} catch (MessagingException e) {
				e.printStackTrace();
			}
		}
		
	}
}
